/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.ziyun.admin.web.config;

import com.google.common.base.Predicate;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.service.Parameter;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger配置
 *
 * @author Mark sunlightcs@gmail.com
 * @since 3.0.0 2018-01-16
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig{

    @Bean
    public Docket createRestApi() {
        List<Parameter> pars = new ArrayList<Parameter>();
        ParameterBuilder appPar = new ParameterBuilder();
        appPar.name("apptype").description("app类型").modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        pars.add(appPar.build());
        ParameterBuilder userPar = new ParameterBuilder();
        userPar.name("userid").description("用户id").modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        ParameterBuilder signPar = new ParameterBuilder();
        signPar.name("sign").description("验签").modelRef(new ModelRef("string")).parameterType("header").required(false)
                .build();
        pars.add(userPar.build());
        pars.add(tokenPar.build());
        pars.add(signPar.build());
        Predicate<RequestHandler> basePackage = RequestHandlerSelectors.basePackage("com.ziyun.quote.module");
  /*      return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(basePackage)
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars)
                .apiInfo(apiInfo());*/

        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            //加了ApiOperation注解的类，生成接口文档
            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
            //包下的类，生成接口文档
            .apis(RequestHandlerSelectors.basePackage("com.ziyun.pop.rest.module"))
            .paths(PathSelectors.any())
            .build()
            .globalOperationParameters(pars);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("紫云POP")
            .description("紫云POP文档")
            .termsOfServiceUrl("http://www.11ziyun.com")
            .version("3.2.0")
            .build();
    }

}